using System.Collections.ObjectModel;
using System.Windows.Controls;
using MyToDo.ViewModels;

namespace MyToDo.Views
{
    /// <summary>
    /// CheckComboBoxDemoView.xaml 的交互逻辑
    /// </summary>
    public partial class CheckComboBoxDemoView : UserControl
    {
        public CheckComboBoxDemoView()
        {
            InitializeComponent();
            // 设置数据上下文
            DataContext = new CheckComboBoxDemoViewModel();
        }

        private void CheckComboBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            //var comboBox = sender as HandyControl.Controls.CheckComboBox;
            //if (comboBox == null) return;

            //// 添加SelectionChanged事件处理程序
            //comboBox.SelectionChanged -= ComboBox_SelectionChanged;
            //comboBox.SelectionChanged += ComboBox_SelectionChanged;

            //// 根据控件名称设置初始选中项
            //var viewModel = DataContext as CheckComboBoxDemoViewModel;
            //if (viewModel == null) return;

            //// 清除所有选中项
            //comboBox.UnselectAll();

            //// 根据控件名称设置初始选中项
            //if (comboBox == FruitsComboBox && viewModel.SelectedFruits != null)
            //{
            //    foreach (var fruit in viewModel.SelectedFruits)
            //    {
            //        comboBox.SelectedItems.Add(fruit);
            //    }
            //}
            //else if (comboBox == CitiesComboBox && viewModel.SelectedCities != null)
            //{
            //    foreach (var city in viewModel.SelectedCities)
            //    {
            //        comboBox.SelectedItems.Add(city);
            //    }
            //}
            //else if (comboBox == ColorsComboBox && viewModel.SelectedColors != null)
            //{
            //    foreach (var color in viewModel.SelectedColors)
            //    {
            //        comboBox.SelectedItems.Add(color);
            //    }
            //}
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //var comboBox = sender as HandyControl.Controls.CheckComboBox;
            //if (comboBox == null) return;

            //var viewModel = DataContext as CheckComboBoxDemoViewModel;
            //if (viewModel == null) return;

            //// 创建新的ObservableCollection来存储选中项
            //var selectedItems = new ObservableCollection<object>();
            //foreach (var item in comboBox.SelectedItems)
            //{
            //    selectedItems.Add(item);
            //}

            //// 根据控件名称更新对应的ViewModel集合
            //if (comboBox == FruitsComboBox)
            //{
            //    viewModel.SelectedFruits = selectedItems;
            //}
            //else if (comboBox == CitiesComboBox)
            //{
            //    viewModel.SelectedCities = selectedItems;
            //}
            //else if (comboBox == LanguagesComboBox)
            //{
            //    viewModel.SelectedLanguages = selectedItems;
            //}
            //else if (comboBox == ColorsComboBox)
            //{
            //    viewModel.SelectedColors = selectedItems;
            //}
        }
    }
}